package cn.yitun.chat.yitunservermanagecore.general.manage.personal.controller;

import cn.yitun.chat.yituncommonmessage.ExceptionEnum;
import cn.yitun.chat.yituncommonmessage.YitunException;
import cn.yitun.chat.yituncommonmessage.result.ResultMessage;
import cn.yitun.chat.yituncommonutil.YitunMD5Util;
import cn.yitun.chat.yituncommonutil.YitunStringUtil;
import cn.yitun.chat.yitungeneral.annotation.codec.Encryped;
import cn.yitun.chat.yitungeneral.annotation.parameter.JsonDefine;
import cn.yitun.chat.yitunservercommonlib.basebean.web.JwtUserDto;
import cn.yitun.chat.yitunservercommonlib.converter.dto2po.user.UserDto2PoConverter;
import cn.yitun.chat.yitunservercommonlib.crud.yitun.BaseController;
import cn.yitun.chat.yitunservercommonlib.dataentity.dto.system.user.ResetPwdDto;
import cn.yitun.chat.yitunservercommonlib.dataentity.dto.system.user.UserDto;
import cn.yitun.chat.yitunservercommonlib.dataentity.po.system.user.UserInfo;
import cn.yitun.chat.yitunservercommonlib.util.PinYinUtil;
import cn.yitun.chat.yitunservercommonlib.util.cache.RedisManager;
import cn.yitun.chat.yitunservermanagecore.general.manage.system.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * 个人信息 业务处理
 *
 * @author yitun
 */
@RestController
@RequestMapping("/manage/personal")
@Slf4j
public class UserProfileController extends BaseController {

    @Autowired
    private UserService userService;

    @Autowired
    private RedisManager redisManager;

    @PostMapping("/user/profile/resetPwd")
    @Encryped(decode = true)
    public RedisManager resetPwd(JwtUserDto jwtUserDto, @JsonDefine ResetPwdDto resetPwdDto) {
        UserInfo user = userService.get(jwtUserDto.getUserId());
        if (resetPwdDto.getId() == null
                || YitunStringUtil.isNotEmpty(resetPwdDto.getNewPassword())
                || YitunStringUtil.isNotEmpty(resetPwdDto.getOldPassword())
                )
            throw new YitunException(ExceptionEnum.REQUEST_PARAM_NULL);
        if ( YitunMD5Util.md5L32(resetPwdDto.getOldPassword()).equals(user.getPassword())) {
            user.setPassword(YitunMD5Util.md5L32(resetPwdDto.getNewPassword()));
            if (userService.updatePassword(user.getId(), user.getPassword()) > 0) {
                return new RedisManager();
            }
        }
        throw new YitunException(ExceptionEnum.UPDATE_PASSWORD_FAILED);
    }


    /**
     * 修改用户
     */
    @PostMapping("/user/profile/update")
    @Encryped(decode = true)
    public ResultMessage update(JwtUserDto jwtUserDto,@JsonDefine UserDto user) {
        UserInfo currentUser = userService.get(jwtUserDto.getUserId());
        if(YitunStringUtil.isNotEmpty(user.getName())) currentUser.setName(user.getName());
        if(YitunStringUtil.isNotEmpty(user.getNickname())) currentUser.setNickname(user.getNickname());
        currentUser.setEmail(user.getEmail());
        if(YitunStringUtil.isNotEmpty(user.getMobile())) currentUser.setMobile(user.getMobile());
        currentUser.setGender(user.getGender());
        currentUser.setHead(user.getHead());
        currentUser.setProvince(user.getProvince());
        currentUser.setCity(user.getCity());
        currentUser.setDistrict(user.getDistrict());
        currentUser.setLiveAddress(user.getLiveAddress());
        currentUser.setBirth(user.getBirth());
        currentUser.setSimpleSpell(PinYinUtil.getFirstSpell(currentUser.getName()));
        currentUser.setSpell(PinYinUtil.getFullSpell(currentUser.getName()));

        if (userService.updatePersonal(UserDto2PoConverter.INSTANCE.do2dto(currentUser), user.getUserHead()) == 0) {
            throw new YitunException(ExceptionEnum.EDIT_FAIL);
        }
        ResultMessage rm = new ResultMessage();
        rm.bodyPut("user", currentUser);
        return rm;
    }

//    /**
//     * 保存头像
//     */
//    @PostMapping("/updateAvatar")
//    public ResultInfo updateAvatar(@RequestParam("avatarfile") MultipartFile file) {
//        User currentUser = userService.get(getCurrentLoginUser().getUserId());
//        try {
//            if (!file.isEmpty()) {
//                String avatar = FileUtil.upload(Global.getAvatarPath(), file);
//                currentUser.setAvatar(avatar);
//                if (userService.updatePersonal(UserConverter.INSTANCE.do2dto(currentUser)) > 0) {
//                    ShiroUtils.setUser(userService.get(currentUser.getUserId()));
//                    return ResultInfo.ok();
//                }
//            }
//        } catch (Exception e) {
//            log.error("修改头像失败！", e);
//        }
//        throw new YitunException(ExceptionEnum.EDIT_FAIL);
//    }
}
